package com.archway.nearshop.zdemo.ademo.service.impl;



import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.archway.nearshop.zdemo.ademo.dao.interfaces.NewsTableMapper;
import com.archway.nearshop.zdemo.ademo.domain.TestTable;
import com.archway.nearshop.zdemo.ademo.service.interfaces.IMyBatisDemoSV;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;


@Transactional
@Service
public class MyBatisDemoSVImpl implements IMyBatisDemoSV {
	

	@Override
	public int normalTest() {
		// TODO Auto-generated method stub
		return 33;
	}
	

	private static Logger log = LoggerFactory.getLogger(MyBatisDemoSVImpl.class);

	@Autowired
	NewsTableMapper mapper;
	
	
	public TestTable getTestById(int id) {
		TestTable table = mapper.getById(id);
		log.info("getTestById-"+table.toString());
		return table;
	}
	
	public List<TestTable> getAll() {
		return mapper.getAll();
	}
	
	public int getAllCount() {
		return mapper.getAllCount();
	}
	
	public List<TestTable> getWithPaginator(PageBounds pageBounds ) {
		return mapper.getWithPaginator(pageBounds);
	}
	
	public int getWithPaginatorCount(PageBounds pageBounds ) {
		return mapper.getWithPaginatorCount(pageBounds);
	}
	
	@Transactional(propagation = Propagation.REQUIRED)
	public int insert() {
		TestTable table = new TestTable();
		table.setTitle("我是标题");
		table.setContent("我是被插入的内容！");
		int rtnNum = mapper.insert(table);
		log.info("insert注意生成主键被返回了：" + table.toString());
		log.info("insert注意受影响行数：" + rtnNum);
		return rtnNum;
	}
	
	@Transactional(propagation = Propagation.REQUIRED)
	public int insertNull(){
		TestTable table = new TestTable();
		table.setTitle("title "+ System.currentTimeMillis());
		int rtnNum = mapper.insertNull(table);
		log.info("insertNull注意查看数据库中空字段是可以被生成的：" + table.toString());
		log.info("insertNull注意受影响行数：" + rtnNum);
		return rtnNum;
	}
	
	@Transactional(propagation = Propagation.REQUIRED)
	public int update(){
		TestTable table = new TestTable();
		table.setId(1);
		table.setContent("kjihgfedcba");
		int rtnNum = mapper.update(table);
		log.info("update注意受影响行数：" + rtnNum);
		return rtnNum;
	}
	
	@Transactional(propagation = Propagation.REQUIRED)
	public int  deleteById(int id){
		int rtnNum = mapper.deleteById(id);
		log.info("deleteById注意受影响行数：" + rtnNum);
		return rtnNum;
	}


}
